package com.example.sqlitedemo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 创建MyHelper对象
        MyHelper myHelper = new MyHelper(this, "itcast.db");
        // 以读写的方式打开数据库
        SQLiteDatabase writableDatabase = myHelper.getWritableDatabase();
        // 关闭数据库
        writableDatabase.close();
    }

    // 向数据表information添加记录
    public void insert(View view) {
        // 创建MyHelper对象
        MyHelper myHelper = new MyHelper(this, "itcast.db");
        // 以读写的方式打开数据库
        SQLiteDatabase writableDatabase = myHelper.getWritableDatabase();

//        writableDatabase.execSQL("insert into information(name,price) " +
//                " values('手机P60',5000)");

        ContentValues contentValues = new ContentValues();
        contentValues.put("name","西瓜");

        writableDatabase.insert("information","price",contentValues);
        // 关闭数据库
        writableDatabase.close();
        Log.e(TAG, "insert: " + "OK");
    }

    public void read(View view) {
        // 创建MyHelper对象
        MyHelper myHelper = new MyHelper(this, "itcast.db");
        // 以读写的方式打开数据库
        SQLiteDatabase db = myHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select _id,name,price from information where _id>?",
                new String[]{"3"});

        //cursor.move(2);// 移到最后一条记录
        String[] columnNames = cursor.getColumnNames(); // 获取所有的字段名
        while (cursor.moveToNext()) {// 移到下一条有效记录
            int _id = cursor.getInt(cursor.getColumnIndex(columnNames[0]));
            String name = cursor.getString(cursor.getColumnIndex(columnNames[1]));
            int price = cursor.getInt(cursor.getColumnIndex(columnNames[2]));
            Log.e(TAG, "read: " + _id + "  " + name + "  " + price);
        }

        // 关闭数据库
        db.close();
    }

    class MyHelper extends SQLiteOpenHelper {
        // 参数包括： 上下文， 数据库名
        public MyHelper(Context context, String name) {
            this(context, name, null, 1);
        }

        public MyHelper(@Nullable Context context,
                        @Nullable String name,
                        @Nullable SQLiteDatabase.CursorFactory factory,
                        int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql = "CREATE TABLE information (" +
                    "  _id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "  name varchar(20)," +
                    "  price INTEGER" +
                    ")";
            db.execSQL(sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }
}